package Modal;

import java.util.ResourceBundle;

public class Receipt {
    private Sale sale;
    private String str;
    private Style style;
    private Printer printer;

    public Receipt(Sale sale) {
        this.sale = sale;

        //利用反射机制读取配置文件
        ResourceBundle resource = ResourceBundle.getBundle("Config/config");
        String styleClass = resource.getString("style");
        String printerClass = resource.getString("printer");
        Class clazz = null;
        try {
            clazz = Class.forName(styleClass);
            style = (Style) clazz.newInstance();
            clazz = Class.forName(printerClass);
            printer = (Printer) clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void print() {
        transToString();
        writeToTxt();
    }

    private void transToString() {
        str = style.transToString(sale);
    }

    private void writeToTxt() {
        printer.writeToTxt(str);
    }
}
